import React from 'react';
import { message } from 'antd';
import { TableMethods, ListParam } from '@/common/Hoc/data';
import PubSub from 'pubsub-js';

export const ListCurd: any = (methods: TableMethods) => (Component: React.ComponentType<any>) => {
  return class extends React.Component<any, ListParam> {
    constructor(props: any) {
      super(props);

      this.state = {
        tableData: [],
        ids: [],
        loading: false,
        expandForm: false,
        modalVisible: false,
        currentData: {},
        initParams: {},
      };
    }

    componentDidMount(): void {
      PubSub.subscribe('refresh', this.refresh);
      this.setState(
        {
          tableData: [],
          ids: [],
          loading: false,
          expandForm: false,
          modalVisible: false,
          currentData: {},
        },
        () => {
          this.initData();
        },
      );
    }
    componentWillUnmount(): void {
      PubSub.unsubscribe('refresh');
    }

    /* 初始化列表 */
    initData = () => {
      this.list();
    };

    /* 刷新列表 */
    refresh = () => {
      this.list();
    };

    /*主键删除*/
    handleDelete = (id: any) => {
      if (methods.getDelete) {
        methods.getDelete({ id: id }).then((res: any) => {
          if (res.status === 0) {
            this.refresh();
            message.success('删除成功');
          } else {
            message.error(res.msg);
          }
        });
      }
    };

    /*模态框显示隐藏*/
    handleModalVisible = (flag?: boolean, record?: any) => {
      this.setState({
        modalVisible: !!flag,
        currentData: record || {},
      });
    };

    getParam = () => {
      let locationParams = this.props.match && this.props.match.params;
      if (!locationParams) {
        locationParams = {};
      }
      return { ...this.state.initParams, ...locationParams };
    };

    list = () => {
      this.setState({
        loading: true,
      });

      if (!methods.getList) {
        return;
      }

      methods
        .getList(this.getParam())
        .then((res: any) => {
          if (res.status === 0) {
            this.setState({
              tableData: res.data,
            });
          } else {
            message.error(res.msg);
          }
          this.setState({
            loading: false,
          });
        })
        .catch((res: any) => {
          this.setState({
            loading: false,
          });
        });
    };

    /* 设置初始化参数 */
    setInitParam = (initParams: any) => {
      this.setState({
        initParams: initParams,
      });
    };

    render() {
      const { tableData, loading, modalVisible, currentData } = this.state;

      return (
        <Component
          {...this.props}
          tableData={tableData}
          tableLoading={loading}
          modalVisible={modalVisible}
          currentData={currentData}
          refresh={this.refresh}
          handleModalVisible={this.handleModalVisible}
          handleDelete={this.handleDelete}
          setInitParam={this.setInitParam}
        />
      );
    }
  };
};
